Skip to content

Fix Metal and CUDA affine quantization for all-negative groups - #4775

Merged
zcbenz merged 3 commits into
ml-explore:mainfrom
lgyStoic:codex/fix-metal-affine-negative-groups
Oct 28, 2026
Merged

zcbenz merged 3 commits into
ml-explore:mainfrom
lgyStoic:codex/fix-metal-affine-negative-groups

Conversation

@lgyStoic

@lgyStoic lgyStoic commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4777

All-negative affine quantization groups on Metal and CUDA retain the initial 0 as their maximum, expanding the quantization range and increasing reconstruction error. Initialize the maximum with negative infinity in both kernels: -Limits<T>::max on Metal and -Limits<float>::max() on CUDA. These floating-point limits return positive infinity, so the new values match the CPU maximum-reduction identity.

Add exact-grid regression coverage with an independent absolute error bound of 1e-5, covering negative, positive, mixed, constant-negative, and zero groups. CPU and Metal cover 7 bit widths (1, 2, 3, 4, 5, 6, 8) × 3 group sizes (32, 64, 128) × 5 patterns = 105 groups. CUDA GPU covers its 6 supported widths × 3 group sizes × 5 patterns = 90 groups; only unsupported 1-bit execution is excluded there. CPU retains 1-bit coverage on CUDA-enabled hosts.

Validation:

  • Built full MLX 0.32.5 with CPU, Metal and Python bindings from the PR head and tested the source-built module on Apple M6.
  • Signed-group regression: all 105 groups passed on both GPU and CPU, including 1-bit.
  • Full python/tests/test_quantized.py on GPU: 57 passed, 1 skipped, using MLX_ENABLE_TF32=0 as in the repository test runner.
  • CUDA was not compiled or run locally.
  • Ruff, clang-format, stream-argument checks, and git diff --check passed.

This patch, tests, validation records, and PR text were prepared with AI assistance.

@lgyStoic

Copy link
Copy Markdown
Contributor Author

@zcbenz Could you take a look at this Metal affine quantization fix? All-negative groups retain the initial zero maximum. This PR fixes the initialization and adds regression coverage.

@lgyStoic lgyStoic changed the title Fix Metal affine quantization for all-negative groups Fix Metal and CUDA affine quantization for all-negative groups Oct 26, 2026

@zcbenz zcbenz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot for locating the bug!

@zcbenz
zcbenz merged commit 32a183e into ml-explore:main Oct 28, 2026
30 checks passed
magicnight pushed a commit to magicnight/mlx that referenced this pull request Oct 29, 2026
…plore#4775)

Co-authored-by: Garry Ling <4526646+lgyStoic@users.noreply.github.com>
(cherry picked from commit 32a183e)
rcfa pushed a commit to cubiculum-ai/mlx that referenced this pull request Nov 26, 2026
…plore#4775)

Co-authored-by: Garry Ling <4526646+lgyStoic@users.noreply.github.com>

Ported: this fork's Metal affine_quantize started w_max at -max for 1-bit
only (bits == 1 ? -Limits<T>::max : 0), from the fork's own one-bit fix.
Apple's line, for every bit width, replaces it. The CUDA kernel and the
test are as in Apple's commit.

(cherry picked from commit 32a183e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Affine quantization expands all-negative groups to zero on Metal and CUDA

2 participants